﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Tools;

namespace Punia
{
    [CommandAttribute(true)]
    public partial class create_contract : BaseForm
    {
        public create_contract()
            : base()
        {
        }

        public create_contract(DbContext dbContext)
            : base(dbContext)
        {
            InitializeComponent();

            cbAuthorization.DataSource = Utility.authorizations(dbContext);
            cbAuthorization.DisplayMember = "Name";

            cbMessages.DataSource = Utility.message_types(dbContext);
            cbMessages.DisplayMember = "Name";
        }

        protected override void Write()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("USE [" + dbContext.Database + "];");
            sb.AppendLine();
            sb.AppendLine("CREATE CONTRACT [" + tbName.Text + "] ");
            if (chAuthorization.Checked)
            {
                sb.AppendLine("AUTHORIZATION [" + cbAuthorization.Text + "]");
            }
            sb.AppendLine("(");

            StringBuilder items = new StringBuilder();

            if (lbInitiator.Items.Count > 0)
            {
                foreach (string item in lbInitiator.Items)
                {
                    items.AppendLine("   [" + item + "] SENT BY INITIATOR,");
                }
            }

            if (lbTarget.Items.Count > 0)
            {
                foreach (string item in lbTarget.Items)
                {
                    items.AppendLine("   [" + item + "] SENT BY TARGET,");
                }
            }

            if (lbAny.Items.Count > 0)
            {
                foreach (string item in lbAny.Items)
                {
                    items.AppendLine("   [" + item + "] SENT BY ANY,");
                }
            }

            if (items.ToString().EndsWith("," + Environment.NewLine))
            {
                int len = items.ToString().Length;

                string m = items.ToString().Substring(0, len - 3);

                sb.AppendLine(m);
            }

            sb.AppendLine(")");

            tbCommand.Text = sb.ToString();
        }

        private void tbName_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void chAuthorization_CheckedChanged(object sender, EventArgs e)
        {
            cbAuthorization.Enabled = chAuthorization.Checked;

            Write();
        }

        private void cbAuthorization_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbDefault_CheckedChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void btnInitiator_Click(object sender, EventArgs e)
        {
            if (cbMessages.Text == string.Empty)
            {
                return;
            }

            lbInitiator.Items.Add(cbMessages.Text);

            Write();
        }

        private void btnTarget_Click(object sender, EventArgs e)
        {
            if (cbMessages.Text == string.Empty)
            {
                return;
            }

            lbTarget.Items.Add(cbMessages.Text);

            Write();
        }

        private void btnAny_Click(object sender, EventArgs e)
        {
            if (cbMessages.Text == string.Empty)
            {
                return;
            }

            lbAny.Items.Add(cbMessages.Text);

            Write();
        }

        private void btnRemoveInitiator_Click(object sender, EventArgs e)
        {
            if (lbInitiator.SelectedIndex == -1) return;

            lbInitiator.Items.RemoveAt(lbInitiator.SelectedIndex);
            Write();
        }

        private void btnRemoveTarget_Click(object sender, EventArgs e)
        {
            if (lbTarget.SelectedIndex == -1) return;

            lbTarget.Items.RemoveAt(lbTarget.SelectedIndex);
            Write();
        }

        private void btnRemoveAny_Click(object sender, EventArgs e)
        {
            if (lbAny.SelectedIndex == -1) return;

            lbAny.Items.RemoveAt(lbAny.SelectedIndex);
            Write();
        }

    }
}